package com.summer.shopping.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.shopping.domain.table.ProductCategory;
import com.summer.shopping.domain.table.SecondaryCategory;
import com.summer.shopping.mapper.ProductCategoryMapper;
import com.summer.shopping.mapper.SecondaryCgMapper;
import com.summer.shopping.service.ProductCategoryService;

import com.summer.shopping.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86136
* @description 针对表【product_category(商品分类表)】的数据库操作Service实现
* @createDate 2025-06-20 13:57:18
*/
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory>
    implements ProductCategoryService{

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private SecondaryCgMapper secondaryCgMapper;
    /**
     * 查询所有分类名称
     * @return
     */
    public List<ProductCategory> getAllCategory() {

        return productCategoryMapper.selectAllCategory();
    }

    /**
     * 新增商品分类
     * @param productCategory
     */
    public void saveCategory(ProductCategory productCategory) {
        productCategoryMapper.saveCategory(productCategory);
    }

    /**
     * 根据一级分类
     * @param id
     */
    public Result deleteByIds(Long id) {
        List<SecondaryCategory> list = new ArrayList<>();

        list = secondaryCgMapper.selectAllSCg(id);
        //判断一下二级分类是否为空，如果不为空查询二级分类下是否有商品
        if (list != null && list.size() > 0){
            return Result.error("请删除该类的所有二级分类");
        }
        SecondaryCategory secondaryCategory = secondaryCgMapper.getSCgById(id);
        secondaryCgMapper.deleteById(secondaryCategory.getId());
        return Result.ok("删除成功");
    }

    /**
     * 更新
     * @param productCategory
     */
    public void updateCategory(ProductCategory productCategory) {
        productCategoryMapper.updateById(productCategory);
    }



}




